package net.fornwall.eclipsecoder.rubysupport;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationType;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.rubypeople.rdt.internal.launching.RubyLaunchConfigurationAttribute;
import org.rubypeople.rdt.internal.launching.RubyRuntime;
import org.rubypeople.rdt.testunit.launcher.TestUnitLaunchConfigurationDelegate;
import org.rubypeople.rdt.testunit.launcher.TestUnitRunnerConfiguration;

/**
 * A class for launching Test::Unit tests. Look at RDT:s TestUnitLaunchShortcut and read up on the
 * eclipse launching framework to get.
 */
class RubyTestLauncher {

    private IFile sourceFile;

    public RubyTestLauncher(IFile sourceFile) {
        this.sourceFile = sourceFile;
    }

    private ILaunchConfigurationWorkingCopy createLaunchWorkingCopy() throws CoreException {
        ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
        ILaunchConfigurationType configType = manager.getLaunchConfigurationType(getLauncherType());
        ILaunchConfigurationWorkingCopy workingCopy = configType.newInstance(null, manager
                .generateUniqueLaunchConfigurationNameFrom(sourceFile.getName())); // //

        if (workingCopy == null) {
            workingCopy = configType.newInstance(null, getLauncherName());
        }
        return workingCopy;
    }

    protected String getLauncherName() {
        return "Test::Unit run";
    }

    protected String getLauncherType() {
        return TestUnitLaunchConfigurationDelegate.ID_TESTUNIT_APPLICATION;
    }

    private String getProjectName() {
        return sourceFile.getProject().getName();
    }

    private void launchDebug() {
        try {
            ILaunchConfigurationWorkingCopy workingCopy = createLaunchWorkingCopy();

            workingCopy.setAttribute(RubyLaunchConfigurationAttribute.PROJECT_NAME,
                    getProjectName());

            workingCopy.setAttribute(RubyLaunchConfigurationAttribute.FILE_NAME,
                    TestUnitRunnerConfiguration.getTestRunnerPath());

            workingCopy.setAttribute(RubyLaunchConfigurationAttribute.WORKING_DIRECTORY, sourceFile
                    .getProject().getLocation().toOSString()); // TestUnitLaunchShortcut.getDefaultWorkingDirectory(sourceFile.getProject()));
            workingCopy.setAttribute(RubyLaunchConfigurationAttribute.SELECTED_INTERPRETER,
                    RubyRuntime.getDefault().getSelectedInterpreter().getName());

            workingCopy.setAttribute(TestUnitLaunchConfigurationDelegate.TESTNAME_ATTR, "");
            workingCopy.setAttribute(TestUnitLaunchConfigurationDelegate.TESTTYPE_ATTR, "");
            workingCopy.setAttribute(TestUnitLaunchConfigurationDelegate.LAUNCH_CONTAINER_ATTR,
                    sourceFile.getName());

            workingCopy.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID,
                    "org.rubypeople.rdt.debug.ui.rubySourceLocator");

            ILaunchConfiguration configuration = workingCopy.doSave();

            configuration.launch(ILaunchManager.RUN_MODE, null);
            // DebugUITools.launch(configuration, ILaunchManager.RUN_MODE);
        } catch (CoreException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        launchDebug();
    }
}
